//
//  SCNBorrowViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/2/6.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNBorrowViewController: SCNTableViewController {
        
    fileprivate var headerView: SCNBorrowHeaderView!
    
    fileprivate var footView: SCNBorrowFootView!
    
    fileprivate var timeData: [SCNBorrowTimeModel]?
    
    fileprivate var typeData: [SCNBorrowTypesModel]?

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let tmpNavImg = UIImage().getImageWithColor(CGSize.init(width: SCNConstants.SCREENWIDTH, height: SCNConstants.NavigationHeight), color: SCNConstants.rootBackgroundColor)
        self.navigationController?.navigationBar.setBackgroundImage(tmpNavImg, for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = NSLocalizedString("我要借款", comment: "Borrow")
        
        self.automaticallyAdjustsScrollViewInsets = false
        
        configurationUI()
        
        request()
    }


    func configurationUI() {
        
        headerView = SCNBorrowHeaderView(frame: CGRect(x: 0, y: 0, width: SCNConstants.SCREENWIDTH, height: 420))
        headerView.getCodeBlock = {
            self.sendSecurityCode($0)
        }
        
        footView = SCNBorrowFootView(frame: CGRect(x: 0, y: 0, width: SCNConstants.SCREENWIDTH, height: 160))
        
        footView.footActionBlock = { [weak self] index in
            if let tmp = self?.typeData {
                
                let obj = tmp[index]
                
                if let tmpUrl = obj.url, let tmpName = obj.name {
                    self?.loadWeb(at: self!, url: tmpUrl, title: tmpName)
                }

            }
        }

        
        self.createTableView(.zero, style: .plain, isRefreshNewData: false, isRefreshMoreData: false) { (table) in
            table.backgroundColor = SCNConstants.colorWithHexString("EEEEEE")
            table.tableHeaderView = self.headerView
            table.tableFooterView = self.footView

        }
        self.tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        tableView.isHidden = false
        
 
    }
    
    override func request() {

        super.request()
        
        SCNBorrowViewModel().requestOtherGetIndex { (result) in
   
            if let tmp = result.borrowtime {
                self.timeData = tmp
            }
            if let tmp = result.types {
                self.typeData = tmp
            }
            self.tableView.reloadData()
        }
    }
}



